<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>运动员列表</title>
    <script src="../../res/js/jquery-3.4.1.min.js"></script>
    <script src="../../res/layui/layui.js"></script>
    <script src="../../res/js/Tools.js"></script>
    <link rel="stylesheet" href="../../res/layui/css/layui.css">
    <script src="../../js/login_common.js"></script>
</head>
<body>


<div style="z-index: 0">
    <table id="demo" lay-filter="test"></table>
</div>

<script type="text/javascript">
    //全局同步
    $.ajaxSetup({
        async: false
    });

</script>

<script>

    var url = "http://127.0.0.1:8900/emp/athletes";

    var coachId =null;
    var table2 = null;
    var form = null;
    layui.use(['table','form'], function() {
        var table = layui.table;

        form = layui.form;

        if(window.loginObj.user.companyNum=="2"){
            coachId = window.loginObj.user.filId;
            url+='?coachId='+coachId;
        }



        //第一个实例
        table2 = table.render({
            elem : '#demo',
            height : 550,
            toolbar : '#toolbarDemo',
            url : url //数据接口
            ,
            page : true //开启分页
            ,
            cols : [ [ //表头
                {
                    type : 'checkbox',
                    fixed : 'left'
                }, {
                    field : 'athleteName',
                    title : '运动员姓名',
                    width : 150
                },{
                    field : 'coachName',
                    title : '教练员',
                    width : 200
                },{
                    field : 'sportName',
                    title : '项目名称',
                    width : 200
                },{
                    field : 'athleteSex',
                    title : '性别',
                    width : 100
                },{
                    field : 'hand',
                    title : '习惯手',
                    width : 100
                }, {
                    field : 'action',
                    title : '操作',
                    width : 200,
                    toolbar : "#barDemo"
                } ] ]
        });

        selectCoach();

        if(window.loginObj.user.companyNum=="2"){
            $("#coachdiv").css("display","none");
            $("#sportdiv").css("display","none");
        }

        //监听工具条
        table.on('tool(test)', function(obj) {

            var data = obj.data;
            if (obj.event === 'detail') {
                layer.msg('ID：' + data.id + ' 的查看操作');
            } else if (obj.event === 'del') {
                layer.confirm('真的删除行么', function(index) {
                    //删除数据在这里可以使用Ajax异步  就和平常使用一样简单

                    var url = "http://127.0.0.1:8900/emp/athlete/"+data.athleteId;

                    $.ajax({
                        url: url,//地址
                        dataType: 'json',//数据类型
                        type: 'DELETE',//类型
                        timeout: 2000,//超时
                        //请求成功
                        success: function (ret, status) {
                            if (ret.num == 1) {
                                layer.msg(ret.msg, {
                                    icon : 1,
                                    time : 1500
                                }, function() {
                                    newLoad();
                                    //obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                                });
                            } else {
                                layer.msg(ret.msg, {
                                    icon : 2,
                                    time : 1500
                                });
                            }
                        },
                        //失败/超时
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            if (textStatus === 'timeout') {
                                layer.msg("请求超时");
                                setTimeout(function () {
                                    layer.msg('重新请求');
                                }, 2000);
                            }
                        }
                    });
                });
            } else if (obj.event === 'edit') {
                layer.open({
                    type : 2,
                    title : '运动员编辑',
                    shadeClose : true,
                    shade : 0.8,
                    area : [ '80%', '90%' ],
                    content : 'atsEmpAthlete.html?id=' + data.athleteId, //iframe的url
                    end : function() {
                    }
                });
            } else if (obj.event === 'select') {
                layer.open({
                    type : 2,
                    title : '运动员查看',
                    shadeClose : true,
                    shade : 0.8,
                    area : [ '80%', '90%' ],
                    content : 'atsEmpAthlete.html?select=1&id=' + data.athleteId, //iframe的url
                    end : function() {
                    }
                });
            }
        });

        $("#close").click(function() {
            $("#divImg").hide(2000);
        });

        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'deleteAllBut':
                    layer.confirm('您真的要删除吗', function(index) {
                        var data = checkStatus.data;
                        var ids = "";
                        $.each(data, function(index, item) {
                            ids += item.athleteId + ",";
                        });

                        var url = "http://127.0.0.1:8900/emp/athletes/"+ids;

                        $.ajax({
                            url: url,//地址
                            dataType: 'json',//数据类型
                            type: 'DELETE',//类型
                            timeout: 2000,//超时
                            //请求成功
                            success: function (obj, status) {
                                layer.msg(obj.msg);
                                if(obj.num==1) {
                                    newLoad();//刷新表格
                                }
                            },
                            //失败/超时
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                if (textStatus === 'timeout') {
                                    layer.msg("请求超时");
                                    setTimeout(function () {
                                        layer.msg('重新请求');
                                    }, 2000);
                                }
                            }
                        });
                    });
                    break;
                case 'addBut':
                    layer.open({
                        type : 2,
                        title : '运动员编辑',
                        shadeClose : true,
                        shade : 0.8,
                        area : [ '700px', '90%' ],
                        content : 'atsEmpAthlete.html', //iframe的url
                        end : function() {
                        }
                    });
                    break;

                case 'seachTable':



                    var name = $("#athleteName").val();
                    var coachId = $("#coachId").val();
                    var sportId = $("#sportId").val();

                    if(window.loginObj.user.companyNum=="2"){
                        coachId = window.loginObj.user.filId;
                    }

                    table2.reload({
                        url: "http://127.0.0.1:8900/emp/athletes",
                        where: {
                            athleteName: name,
                            coachId: coachId,
                            sportId: sportId
                        },
                        page: { curr: 1 }
                    });

                    selectCoach(coachId,sportId);

                    if(window.loginObj.user.companyNum=="2"){
                        $("#coachdiv").css("display","none");
                        $("#sportdiv").css("display","none");
                    }

                    $("#athleteName").val(name);
                    break;
            }
        });
    });



    /**
     * 用在新增或者修改界面   由子窗口调用
     */
    function newLoad() {

        var name = $("#athleteName").val();
        var coachId = $("#coachId").val();
        var sportId = $("#sportId").val();

        layui.table.reload("demo");

        if(window.loginObj.user.companyNum=="2"){
            $("#coachdiv").css("display","none");
            $("#sportdiv").css("display","none");
        }

        $("#athleteName").val(name);
        selectCoach(coachId,sportId);
    };

    function selectCoach(coachId,sportId) {
        $.ajax({
            url: "http://127.0.0.1:8900/emp/getcoachs",//地址
            dataType: 'json',//数据类型
            type: 'POST',//类型
            timeout: 2000,//超时
            //请求成功
            success: function (obj, status) {
                $.each(obj,function (i,item) {
                    $("#coachId").append("<option value='"+item.coachId+"'>"+item.coachName+"</option>");
                });

                $("#coachId").val(coachId);
                form.render();
            },
            //失败/超时
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                if (textStatus === 'timeout') {
                    layer.msg("请求超时");
                    setTimeout(function () {
                        layer.msg('重新请求');
                    }, 2000);
                }
            }
        });

        url = getRootPath() + "AtsBaseSportController/findAll";
        data = null;
        $.post(url,data,function (obj) {
            $.each(obj,function (i,item) {
                $("#sportId").append("<option value='"+item.sportId+"'>"+item.sportName+"</option>");
            });

            $("#sportId").val(sportId);

            form.render();
        },"json");
    }
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="select">查看</a>
</script>

<script type="text/html" id="toolbarDemo">
    <div>
        <div class="layui-inline">
            <input class="layui-input" name="athleteName" id="athleteName" autocomplete="off" placeholder="运动员姓名">
        </div>

        <div  id="coachdiv" style="display: inline-block">
            <select name="coachId" id="coachId" lay-verify="required" lay-search="">
                <option value="">直接选择或搜索选择教练员</option>
            </select>
        </div>

        <div  id="sportdiv" style="display: inline-block">
            <select name="sportId" id="sportId" lay-verify="required" lay-search="">
                <option value="">直接选择或搜索选择训练项目</option>
            </select>
        </div>

        <button class="layui-btn layui-btn-sm"  lay-event="seachTable">搜索</button>
        <button class="layui-btn layui-btn-sm" lay-event="deleteAllBut">删除选中数据</button>
        <button class="layui-btn layui-btn-sm"  lay-event="addBut">增加数据</button>
    </div>
</script>
</body>
</html>